//
//  SJumpUtil.swift
//  niuguwang
//
//  Created by BrightLi on 2021/5/24.
//  Copyright © 2021年 taojinzhe. All rights reserved.
//

import UIKit

@objcMembers open class SJumpUtil:NSObject
{
    dynamic public class func topVC(_ target:UIViewController!) -> UIViewController?
    {
        if(target is UINavigationController){
            return topVC(((target as? UINavigationController)?.topViewController)!)
        }
        if(target is UITabBarController){
            return topVC(((target as? UITabBarController)?.selectedViewController)!)
        }
        return target
    }
    
    dynamic public class func topVC() -> UIViewController?
    {
        guard let rootVC = UIApplication.shared.keyWindow?.rootViewController else{
            return nil
        }
        guard let tabVC = rootVC as? UITabBarController else{
            return rootVC
        }
        guard let nav=tabVC.selectedViewController as? UINavigationController else{
            return tabVC.selectedViewController
        }
        if let topVC=nav.visibleViewController{
            return topVC
        }
        return nil
    }
    
    dynamic public class func isTop(_ target:UIViewController?)->Bool
    {
        guard let topVC = target?.navigationController?.topViewController else{
            return false
        }
        if topVC != target{
            return false
        }
        return true
    }
    
    dynamic public class func isTopProtocol(_ proto:Protocol) -> UIViewController?
    {
        if let topVC = self.topVC(){
            if topVC.conforms(to: proto){
                return topVC
            }
        }
        return nil
    }
    
    dynamic public class func isTopVC(_ cls:AnyClass) -> UIViewController?
    {
        if let topVC = self.topVC(){
            if topVC.isKind(of: cls){
                return topVC
            }
        }
        return nil
    }
    // 当前视图是否为指定的类
    dynamic public class func isTopList(_ list:[AnyClass]) -> Bool
    {
        for klass in list{
            if isTopVC(klass) != nil{
                return true
            }
        }
        return false
    }
    // 是否为指定的父视图
    dynamic public class func isParentVC(_ target:UIViewController,cls:AnyClass) ->UIViewController?
    {
        if let parentVC = target.parent{
            if parentVC.isKind(of: cls){
                return parentVC
            }
        }
        return nil
    }
    // 是否有父视图
    dynamic public class func hasParentVC(_ target:UIViewController)->Bool{
        if let parent = target.parent{
            if parent is UINavigationController{
                return false
            }
            return true
        }
        return false
    }
    
    dynamic public class func rootVC() -> UIViewController!
    {
        guard let rootVC = UIApplication.shared.keyWindow?.rootViewController else{
            return nil
        }
        guard let tabVC = rootVC as? UITabBarController else{
            return rootVC
        }
        guard let nav=tabVC.selectedViewController as? UINavigationController else{
            return rootVC
        }
        if let firstVC=nav.viewControllers.first{
            return firstVC
        }
        return rootVC
    }
    
    dynamic public class func popTopVC()
    {
        if let topVC = self.topVC(),let nav = topVC.navigationController
        {
            nav.popViewController(animated: true)
        }
    }
    
    dynamic public class func popToRoot(_ target:UINavigationController)
    {
        let list=target.viewControllers
        if list.count == 1{
            return
        }
        if let first=list.first{
            target.setViewControllers([first], animated:false)
        }
    }
    
    dynamic public class func pushVC(_ target:UIViewController?)
    {
        pushVC(target,animated:true)
    }
    
    dynamic public class func pushVC(_ target:UIViewController?,animated:Bool = true)
    {
        if target == nil{
            return
        }
        var nav:UINavigationController?
        nav = topVC() as? UINavigationController
        if nav == nil{
            nav = topVC()?.navigationController
        }
        if nav == nil{
            return
        }
        nav!.pushViewController(target!, animated: animated)
    }
    
    dynamic public class func pushVC(_ current:UIViewController?,target:UIViewController,animated:Bool = true)
    {
        var nav = current?.navigationController
        if nav == nil{
            nav = topVC() as? UINavigationController
        }
        if nav == nil{
            nav = topVC()?.navigationController
        }
        if nav == nil{
            return
        }
        nav!.pushViewController(target, animated: animated)
    }
    
    dynamic public class func currentTopNav()->UINavigationController?
    {
        var nav:UINavigationController?
        nav = topVC() as? UINavigationController
        if nav == nil{
            nav = topVC()?.navigationController
        }
        if nav == nil{
            return nil
        }
        return nav;
    }
    
    dynamic public class func rootNav(_ target:UIViewController?)->UINavigationController?
    {
        if target == nil{
            return nil
        }
        let nav = UINavigationController.init(rootViewController: target!)
        return nav
    }
    // 关闭VC
    dynamic public class func closeVC(_ current:UIViewController?)
    {
        if current == nil{
            return
        }
        if let nav = rootVC()?.navigationController{
            var list=nav.viewControllers
            for (index,value) in list.enumerated(){
                if value == current{
                    list.remove(at: index)
                    break
                }
            }
            nav.setViewControllers(list, animated:false)
        }
    }
    // 关闭当前页跳到新页面
    dynamic public class func closeAndPushVC(_ target:UIViewController,animated:Bool = true)
    {
        SJumpUtil.pushVC(target,animated:animated)
        if let nav = rootVC()?.navigationController{
            var temp=nav.viewControllers
            if temp.count>2{
                temp.remove(at: temp.count-2)
                nav.viewControllers=temp
            }
        }
    }
    
    dynamic public class func presentVC(_ current:UIViewController?,target:UIViewController?,animated:Bool)
    {
        if current == nil || target == nil{
            return
        }
        if current!.presentedViewController == target{
            return
        }
        current!.present(target!, animated: animated){}
    }
    
    dynamic public class func presentVC(_ target:UIViewController?,animated:Bool)
    {
        if target == nil{
            return
        }
        guard let current = topVC() else{
            return
        }
        if current.presentedViewController == target{
            return
        }
        current.present(target!, animated: animated){}
    }
    
    dynamic public class func dismiss(_ target:UIViewController?,animated:Bool=true)
    {
        if target == nil{
            return
        }
        DispatchQueue.safeMainAsync{
            target!.dismiss(animated: animated, completion: nil)
        }
    }
    
    dynamic public class func popVC(_ target:UIViewController?,animated:Bool=true)
    {
        if target == nil{
            return
        }
        if let nav = target!.navigationController{
            nav.popViewController(animated:animated);
        }else if let topVC = self.topVC(){
            topVC.navigationController?.popViewController(animated:animated)
        }
    }
    // 关闭指定类的控制器
    dynamic public class func closeVCClass(_ target:AnyClass)
    {
        if let topVC = isTopVC(target){
            closeVC(topVC)
        }
    }
    // 前一视图是否为指定的类
    dynamic public class func isPrevVC(_ klass:AnyClass) -> Bool{
        guard let topVC = self.topVC() else{
            return false
        }
        guard let list = topVC.navigationController?.viewControllers else{
            return false
        }
        if list.count<2{
            return false
        }
        guard let prev = list[safe:list.count-2] else{
            return false
        }
        if prev.isKind(of: klass){
            return true
        }
        return false
    }
    // 前一视图是否为指定的类
    dynamic public class func isPrevList(_ list:[AnyClass]) -> Bool
    {
        for klass in list{
            if isPrevVC(klass){
                return true
            }
        }
        return false
    }
    // 前一视图是否为指定的类
    dynamic public class func prevVC(_ cls:AnyClass) -> UIViewController?{
        guard let topVC = self.topVC() else{
            return nil
        }
        guard let list = topVC.navigationController?.viewControllers else{
            return nil
        }
        if list.count<2{
            return nil
        }
        guard let prev = list[safe:list.count-2] else{
            return nil
        }
        if prev.isKind(of: cls){
            return prev
        }
        return nil
    }
    
    @objc class func hasVC(_ cls:AnyClass)->Bool
    {
        if let nav = rootVC()?.navigationController{
            let vcs=nav.viewControllers
            for target in vcs{
                if target.isKind(of: cls){
                    return true
                }
            }
        }
        return false
    }
    
    dynamic public class func openURL(_ path:String){
        if let url=URL(string:path){
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options:[:]) { (success) in
                    if(!success){
                        let info = String(format:"path=%@",path)
                        SLog.debug("warn_open_url:%@",info)
                    }
                }
            } else {
                UIApplication.shared.openURL(url)
            }
        }
    }
    
    dynamic public class func hasPop(_ cls:AnyClass)->UIView?
    {
        let window=SJumpUtil.topWindow()
        for view in window.subviews{
            if view.isKind(of: cls){
                return view
            }
        }
        return nil
    }
    // 获得最顶层的UIWindow
    dynamic public class func topWindow()->UIWindow
    {
        if let KeyboardWindow = NSClassFromString("UIRemoteKeyboardWindow") as? UIWindow.Type{
            if let windows = UIApplication.shared.windows as [UIWindow]?{
                for(_,window) in windows.enumerated().reversed(){
                    if !window.isHidden && window.isKind(of:KeyboardWindow){
                        return window
                    }
                }
            }
        }
        return UIApplication.shared.keyWindow!
    }
}
